;(function () { 
	var vueInput = {} 
	vueInput.install = function (Vue,options) { 
		Vue.directive('input', { 
			params: ['number','length','letter'],
			update:function(value){
				var _this = this;
				
				this.callBack = value;
				this.el.addEventListener('input',function(){
					_this.inputFun(this);
				},false)
				
				
				this.el.addEventListener('input',function(){
					_this.inputFun(this);
				},false)
				
				
			},
			inputFun:function(element){
				var inputStr = element.value;
				
				if(this.params.number && this.params.letter){
					//element.value = inputStr = inputStr.replace(/[^0-9a-zA-Z]/g, '');
					
					this.replaceAndSetPos(this.el,/[^\a-\z\A-\Z0-9]/g,'');
				}else if(this.params.letter){
					element.value = inputStr = inputStr.replace(/[^a-zA-Z]/g, '');
				}else if(this.params.number){
					element.value = inputStr = inputStr.replace(/[^0-9]/g, '');
				}
				this.callBack(element.value);
				
			},
			
			/*获取光标位置*/  
			getCursorPos(obj)  {   
			    var CaretPos = 0;   
			    // IE Support   
			    if (document.selection) {   
			        obj.focus (); //获取光标位置函数   
			        var Sel = document.selection.createRange ();   
			        Sel.moveStart ('character', -obj.value.length);  
			        CaretPos = Sel.text.length;   
			    }   
			    
			    else if (obj.selectionStart || obj.selectionStart == '0')   
			        CaretPos = obj.selectionEnd;   
			    return (CaretPos);   
			},
			
			/*  
			定位光标  
			*/   
			setCursorPos(obj,pos){   
			    if(obj.setSelectionRange) { //Firefox/Safari/Chrome/Opera  
			        obj.focus(); //  
			        obj.setSelectionRange(pos,pos);   
			    } else if (obj.createTextRange) { // IE  
			        var range = obj.createTextRange();   
			        range.collapse(true);   
			        range.moveEnd('character', pos);   
			        range.moveStart('character', pos);   
			        range.select();   
			    }   
			}, 
			  
			  
			/*  
			替换后定位光标在原处,可以这样调用onkeyup=replaceAndSetPos(this,/[^/d]/g,'');  
			*/   
			replaceAndSetPos(obj,pattern,text){   
			  /*if(event.shiftKey||event.altKey||event.ctrlKey||event.keyCode==16||event.keyCode==17||event.keyCode==18||(event.shiftKey&&event.keyCode==36))  
			   return; */  
			      
			    var pos=this.getCursorPos(obj);//保存原始光标位置   
			  
			  
			    var temp=obj.value; //保存原始值   
			  
			  
			    obj.value=temp.replace(pattern,text);//替换掉非法值   
			      
			    //截掉超过长度限制的字串（此方法要求已设定元素的maxlength属性值）  
			    var max_length = obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";  
			    if( obj.value.length > max_length){  
			        var str1 = obj.value.substring( 0,pos-1 );  
			        var str2 = obj.value.substring( pos,max_length+1 );  
			        obj.value = str1 + str2; 
			    }  
			      
			    pos=pos-(temp.length-obj.value.length);//当前光标位置   
			    this.setCursorPos(obj,pos);//设置光标   
			},  
			
			
			
			unbind:function(){
				//清除绑定键盘事件
				this.el.removeEventListener('input',function(){
					_this.input(this);
				},false);
			}
		});
	} 
	if (typeof exports == "object") {
		module.exports = vueInput;
	} else if (typeof define == "function" && define.amd) {
		define([], function(){ 
			return vueInput ;
		})
	} else if (window.Vue) { 
		window.vueInput = vueInput;
		Vue.use(vueInput);
	}
})();
